﻿namespace DepartmentSpecificStoreView
{
    using System;
    using System.Collections.Generic;
    using System.Data;

    using Microsoft.SharePoint;

    public class DataDisplayProcess
    {
        #region Methods

        public static DataTable CreateBaseDepartmentSpecificColumns( DataTable baseColumnsTable, SPWeb storeWeb, string listName )
        {
            TargetFieldsDataCollectionBL allFieldsProcessing = new TargetFieldsDataCollectionBL( );

            if ( listName.Equals( string.Empty ) )

                return baseColumnsTable;
            else
                baseColumnsTable = allFieldsProcessing.GetAllBaseDepartmentSelectedFields( baseColumnsTable, storeWeb, listName );

            return baseColumnsTable;
        }

        public static DataTable CreateColumns( )
        {
            DataTable dt = new DataTable( );

            dt.Columns.Add( "Edit Link", typeof( string ) );
            dt.Columns.Add( "ListName", typeof( string ) );
            dt.Columns.Add( "ListGUID", typeof( string ) );
            dt.Columns.Add( "Project", typeof( String ) );
            dt.Columns.Add( "Title", typeof( String ) );
            dt.Columns.Add( "State", typeof( String ) );

            return dt;
        }

        public static DataTable CreateDepartmentSpecificColumns( DataTable baseColumnsTable, string sourceDepartment, SPWeb storeWeb )
        {
            List<DepartmentVisibleFields> allFieldsForSource = new List<DepartmentVisibleFields>( );
            TargetFieldsDataCollectionBL allFieldsProcessing = new TargetFieldsDataCollectionBL( );

            if ( sourceDepartment.Equals( string.Empty ) )
                return null;
            else
            {
                allFieldsProcessing.GetAllDepartmentSelectedFields( storeWeb, sourceDepartment );

                allFieldsForSource = allFieldsProcessing.AvailableFields;
            }

            List<DepartmentVisibleFields> filteredFieldResults = allFieldsForSource;
            string departmentAbriviation = "";

            foreach ( DepartmentVisibleFields deptField in filteredFieldResults )
            {
                try
                {
                    baseColumnsTable.Columns.Add( deptField.TargetFieldName, typeof( string ) );
                }
                catch ( Exception )
                {
                    if ( deptField.TargetDepartmentName.Trim( ).Equals( "Stores Detail" ) )
                        departmentAbriviation = "Base";
                    else
                        departmentAbriviation = allFieldsProcessing.GetDepartmentAbbriviations( storeWeb, deptField.TargetDepartmentName );

                    baseColumnsTable.Columns.Add( deptField.TargetFieldName + " - " + departmentAbriviation, typeof( string ) );
                }
            }

            return baseColumnsTable;
        }

        public static DataRow PopulateBaseStoreColumns( DataRow currentRow )
        {
            return currentRow;
        }

        #endregion Methods
    }
}